package view;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JTextField;
import javax.swing.event.DocumentListener;

/**
 * The {@code NumericalTextField} class creates a {@code JTextField} that
 * can show and clear errors by changing its {@code Color}. It can add a
 * {@code DocumentListener} to be controlled properly.
 * @author Oliver Eckstein
 *
 */
public class NumericalTextField extends JTextField implements FocusListener {

    private static final long serialVersionUID = 1L;
    
    /**
     * A flag that indicates if this text field shows an error.
     */
    private boolean showsError = false;
    
    /**
     * A flag that indicates if this text field has only numerical input.
     */
    private boolean isNumerical = false;
    
    /**
     * The ID of the referenced parameter.
     */
    private String parameterID;
    
    /**
     * To create a new {@code NumericalTextField}.
     * @param parameterID the parameter's ID that gets controlled by this
     * text field.
     */
    public NumericalTextField(String parameterID) {
        super(6);
        this.parameterID = parameterID;
        this.addFocusListener(this);
    }
    
    /**
     * Indicates that the input caused an error.
     */
    public void showError()
    {
        setBackground(Color.RED);        
        showsError = true;
    }
    
    /**
     * Removes the error indication.
     */
    public void clearError()
    {
        setBackground(Color.WHITE);
        showsError = false;
    }
    
    /**
     * To get the actual error status of this {@code NumericalTextField}.
     * @return error status
     */
    public boolean getErrorStatus()
    {
        return showsError;
    }
    
    /**
     * To check if the input of a text field is a numerical value.
     * @return text field carries numerical input: {@code true};
     * text field does not carry numerical input: {@code false}
     */
    public boolean isNumerical() {
        return isNumerical;
    }
    
    /**
     * To set, if a text field's input is numerical or not.
     * @param isNumerical text field carries numerical input: {@code true};
     * text field does not carry numerical input: {@code false}
     */
    public void setIsNumerical(boolean isNumerical) {
        this.isNumerical = isNumerical;
    }
    
    /**
     * To add a {@code DocumentListener} to this {@code NumericalTextField}.
     * @param theController the controller which controls this
     *  {@code NumericalTextField}. 
     */
    public void addDocumentListener(DocumentListener theController)
    {
        getDocument().addDocumentListener(theController);
    }

    @Override
    public void focusGained(FocusEvent e) {
        this.selectAll();
    }

    @Override
    public void focusLost(FocusEvent e) {        
        // Nothing to do here.
    }
    
    public String getParameterID() {
    	return parameterID;
    }
}
